package com.flin.icesilkworm.examination.bg.controller;


import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.flin.icesilkworm.analyze.syntax.ElEngine;
import com.flin.icesilkworm.examination.bas.form.TheTakeExamForm;
import com.flin.icesilkworm.examination.bas.form.ext.ForGridTheQuestionsAnswerForm;
import com.flin.icesilkworm.examination.bg.form.ByExamStandardForm;
import com.flin.icesilkworm.examination.bg.service.IExamStandardService;
import com.flin.icesilkworm.framework.pojos.ResultMessage;
import com.flin.icesilkworm.framework.spring.BaseSpringController;
import com.flin.icesilkworm.framework.spring.SpringWebUtils;
import com.flin.icesilkworm.framework.spring.auth.Auth;

import java.math.BigDecimal;

@Controller
@Auth(authCode="bg_standard",authDesc="标准操作",ctrl=true)
@RequestMapping("/bg/standard")
public class ExamStandardController extends BaseSpringController{

	@Resource(name="examStandardService")
	private IExamStandardService examStandardService;
	
	@PostConstruct
	public void Init(){
	    super.Init();
	    try {
			//初始化的时候增加全局方法 
			ElEngine.getInstance().addJselMethod("getTeacherId", this.getClass().getDeclaredMethod("getTeacherId"));
		} catch ( Exception e) {
			log.error(e);
		}
		 
	}
	
	public static Long getTeacherId(){
		Long userid = SpringWebUtils.getUserIdByRole("3");
		if(userid == null) return -1L;
		return userid;
	}

	@Auth(authCode="loginByAdmin",authDesc="用户登录",ctrl=false)
	@RequestMapping("/loginByAdmin.do")
	public @ResponseBody ResultMessage loginByAdmin(@RequestBody @Valid ByExamStandardForm form){
		
		Map<String,Object> map = (examStandardService.loginByAdmin(form.getUsername(),form.getPassword(),form.getType()));
		if(map.containsKey("userId")) {
			ResultMessage rm =  success("登录成功", map);
			rm.setToken("" +map.get("token"));
			return rm;
		}
		return fail("登录失败");
		
	}
	
	 /**
	* Description:
	*     参加考试
	* AccessUrl:
	*     /joinExamTake.do
	* Parameters:
	*     t : 考生应试对象
	* Return:    
	*	  json:{success:false,message:''}
	* History: 
	*     1.00  auto_time  sybris   Creation
	* Example:
	*/
	@Auth(authCode="joinExamTake",authDesc="增加实体对象'考生应试'",ctrl=true)
	@RequestMapping("/joinExamTake.do")
	public @ResponseBody ResultMessage joinExamTake(@RequestBody @Valid TheTakeExamForm f){
		
		f.getDescObject().setExameeId(SpringWebUtils.getUserId());//设置考生主键

		return success("参加考试",examStandardService.joinExamTake(f.getDescObject()));
	}	
	

	 /**
	* Description:
	*     记录答案或者提交
	* AccessUrl:
	*     /recordVolume.do
	* Parameters:
	*     t : 考生应试对象
	* Return:    
	*	  json:{success:false,message:''}
	* History: 
	*     1.00  auto_time  sybris   Creation
	* Example:
	*/
	@Auth(authCode="recordVolume",authDesc="记录答案或者提交",ctrl=true)
	@RequestMapping("/recordVolume.do")
	public @ResponseBody ResultMessage recordVolume(@RequestBody @Valid ForGridTheQuestionsAnswerForm f){
		return success("保存答题",examStandardService.recordVolume(f.getTheQuestionsAnswers().stream().map(t->t.getDescObject()).collect(Collectors.toList()) ,SpringWebUtils.getUserId(),f.getEndTakeExam(),f.getTakeId()));
	}	


	@Auth(authCode="learningRecord",authDesc="考生学习课程",ctrl=true)
	@RequestMapping("/learningRecord.do")
	public @ResponseBody ResultMessage learningRecord(@RequestBody @Valid ByExamStandardForm form){
			return new ResultMessage(examStandardService.learningRecord(form.getProId()));
	}

	@Auth(authCode="learningDetails",authDesc="考生学习章节",ctrl=true)
	@RequestMapping("/learningDetails.do")
	public @ResponseBody ResultMessage learningDetails(@RequestBody @Valid ByExamStandardForm form){
			return new ResultMessage(examStandardService.learningDetails(form.getRecordId(),form.getDetailId(),form.getContent(),form.getTime()));
	}

	@Auth(authCode="viewTakeExam",authDesc="考生查看结果",ctrl=true)
	@RequestMapping("/viewTakeExam.do")
	public @ResponseBody ResultMessage viewTakeExam(@RequestBody @Valid ByExamStandardForm form){
			return new ResultMessage(examStandardService.viewTakeExam(form.getTakeId()));
	}

	@Auth(authCode="getStatisticInfos",authDesc="统计数据",ctrl=true)
	@RequestMapping("/getStatisticInfos.do")
	public @ResponseBody ResultMessage getStatisticInfos(@RequestBody @Valid ByExamStandardForm form){
			return new ResultMessage(examStandardService.getStatisticInfos());
	}

}
